package com.nantai.jiafang.nantaijiafang.deviceManager.devicemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.ReasonAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.TextAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.ReasonBen;
import com.nantai.jiafang.nantaijiafang.deviceManager.constant.Constans;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.Error;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.HttpClient;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.ResponseHandler;
import com.nantai.jiafang.nantaijiafang.deviceManager.preference.PreferencesUtil;
import com.nantai.jiafang.nantaijiafang.deviceManager.utils.MessagePop;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

public class PutOutActivity extends BaseActivity {

    @InjectView(R.id.code_tx)
    EditText mCodeTx;
    @InjectView(R.id.scan_btn)
    Button mScanBtn;
    @InjectView(R.id.type_spinner)
    Spinner mTypeSpinner;
    @InjectView(R.id.place_tx)
    EditText mPlaceTx;
    @InjectView(R.id.action_btn)
    Button mActionBtn;
    @InjectView(R.id.tv_toolbar_title)
    TextView mTvToolbarTitle;
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.scrap_reason_et)
    TextView mScrapReasonEt;
    @InjectView(R.id.scrap_back)
    LinearLayout mScrapBack;

    private String operate_type = null;
    private MaterialDialog showDialog = null;
    private String reasonId=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_put_out);
        ButterKnife.inject(this);
        mTvToolbarTitle.setText("出库");
        mToolbar.setNavigationIcon(R.drawable.md_nav_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        List mItems= Arrays.asList("维修出库", "销售出库", "报废出库");
        SpinnerAdapter _adapter = new TextAdapter(this,mItems);
        mTypeSpinner.setAdapter(_adapter);
        mTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,int pos, long id) {
                operate_type = String.valueOf(pos + 4);
                if (pos == 2) {
                    showScrapReason(true);
                }else
                {
                    showScrapReason(false);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }

        });
        mTypeSpinner.setSelection(0);
        showDialog = new MaterialDialog.Builder(PutOutActivity.this)
                .content("正在请求出库")
                .progress(true, 0, false)
                .cancelable(false).build();


    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent =getIntent();
        String reasonid= intent.getStringExtra("reasonId");
        String content = intent.getStringExtra("content");
        if(content!=null)
        {
            mScrapReasonEt.setText(content);
        }
        if(reasonid!=null)
        {
            reasonId=reasonid;
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            setIntent(intent);
        }
    }

    private void showScrapReason(boolean show) {
        if(show)
             mScrapBack.setVisibility(View.VISIBLE);
        else
            mScrapBack.setVisibility(View.GONE);
    }

    @OnClick({R.id.scan_btn, R.id.action_btn,R.id.scrapSelect})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.scrapSelect:
            {
                Intent intent = new Intent(PutOutActivity.this, ReasonActivity.class);
                intent.putExtra("reasonType", "1");
                if(!(mCodeTx.getText() == null || mCodeTx.getText().toString().equals("")))
                {
                    String deviceType= mCodeTx.getText().toString().substring(0,3);
                    intent.putExtra("deviceType",deviceType);
                }
                startActivity(intent);
            }
            break;
            case R.id.scan_btn: {
                Intent intent = new Intent(PutOutActivity.this, CaptureActivity.class);
                intent.putExtra("operate_type", String.valueOf(Constans.ScanCode));
                startActivityForResult(intent, 20);
            }
            break;
            case R.id.action_btn: {
                if (mCodeTx.getText() == null || mCodeTx.getText().toString().equals("")) {
                    MessagePop.ToastMessage(PutOutActivity.this, "请输入设备条码");
                    return;
                }
                if (operate_type == null) {
                    MessagePop.ToastMessage(PutOutActivity.this, "请选择出库方式");
                    return;
                }
                if (mPlaceTx.getText() == null || mPlaceTx.getText().toString().equals("")) {
                    MessagePop.ToastMessage(PutOutActivity.this, "请输入出库地点");
                    return;
                }

                showDialog.show();
                Map<String, String> params = new HashMap<>();
                String qrcode = mCodeTx.getText().toString();
                params.put("qrcode", qrcode);
                params.put("operate_type", operate_type);
                params.put("place_name", mPlaceTx.getText().toString());
                if(operate_type.equals("6"))
                {
                    if(reasonId==null)
                    {
                        MessagePop.ToastMessage(PutOutActivity.this, "请选择报废原因");
                        return;
                    }
                    params.put("reason", reasonId);
                }
                HttpClient client = new HttpClient(PutOutActivity.this);
                Call call = client.service().putIn(params);
                client.request(call, new ResponseHandler<ResponseBody>() {
                    @Override
                    public void onSuccess(ResponseBody res) {
                        showDialog.dismiss();
                        MessagePop.ToastMessage(PutOutActivity.this, "出库成功");
                    }

                    @Override
                    public void onFailure(int code, Error e) {
                        showDialog.dismiss();
                        if (code != 404) {
                            MessagePop.ToastMessage(PutOutActivity.this, e.getMessage());
                        } else {
                            MessagePop.ToastMessage(PutOutActivity.this, "网络异常");
                        }
                    }
                });
            }
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //可以根据多个请求代码来作相应的操作
        if (20 == resultCode) {
            String qrcode = data.getStringExtra("qrcode");
            mCodeTx.setText(qrcode == null ? "" : qrcode);
        }
    }
}
